var express = require('express');
var router = express.Router();
var hget = require('../util/http_get_util.js');
var hpost = require('../util/http_post_util.js');

// 查询功能
router.get('/', function (req, res, next) {
    hget(`http://localhost:9999/QueryInfo`, {}).then(function (data) {
        var obj = JSON.parse(data);
        if (obj.status == 1) {
            res.render('four1', obj);
        } else if (obj.status == 2) {
            console.log(obj.message);
        }
    }, function (e) {
        console.log(e.statusCode, e.statusMessage);
    });
});


//修改功能
//1.通过ID获取数据库中的信息
//2.再将信息渲染到页面
router.get('/update_for', function (req, res, next) {
    hget(`http://localhost:9999/Get_User_By_Id?user_id=${req.param("id")}`)
        .then(function (data) {
            res.render('four2', JSON.parse(data));
        }, function (e) {
            console.log(e.statusCode, e.statusMessage)
        });
});
//3.提交信息的修改
router.post('/update_Info', function (req, res, next) {
    hpost('/Submit_Update', req.body)
        .then(function (data) {
            res.send(data);
            var obj = JSON.parse(data);
            if (obj && obj.status == 1) {
                res.send("<h1>Success to modify</h1>");
                // res.redirect('/psl1')//跳转到 psl1.ejs
            } else {
                res.send("<h1>Failure to modify</h1>");
            }
        }, function (e) {
            console.log(e);
            console.log(e.statusCode, e.statusMessage);
        });
});

//删除功能
            router.get('/delete_for', function (req, res, next) {

                hget(`http://localhost:9999/Delete_Info?user_id=${req.param("id")}`)
                    .then(function (data) {
                        debugger;
                        var obj = JSON.parse(data);

                        if (obj && obj.status == 1) {
                            res.send("<h1>Success to modify</h1>");
                        } else {
                            res.send("<h1>Failure to modify</h1>");
                        }
        }, function (e) {
            console.log(e);
            res.send(`${e}`)
        });
});

module.exports = router;